package leetCode.thread.leetCode1115;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * 利用阻塞队列实现
 */
public class FooBar2 implements FooBar{

    private int n;
    private final BlockingQueue<Integer> bar = new PriorityBlockingQueue<>(1);
    private final BlockingQueue<Integer> foo = new PriorityBlockingQueue<>(1);



    @Override
    public void foo(Runnable printFoo) throws InterruptedException {
        for(int i=0;i<n;i++) {
            foo.put(1);
            printFoo.run();
            bar.put(1);
        }
    }

    @Override
    public void bar(Runnable printBar) throws InterruptedException {
        for(int i=0;i<n;i++){
            bar.take();
            printBar.run();
            foo.take();
        }

    }
}
